package com.cy.pj.module.controller;

import com.cy.pj.module.pojo.RequestParameter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;


@RestController
public class ParamObjectController {
    //以直接量的方式接受请求中的参数
    @RequestMapping("/doHandleRequestParam01")
    public String doHandleRequestParam01(String name){
        return "request params is " + name;
    }
    @RequestMapping("/doHandleRequestParam02")
    public String doHandleRequestParam01(String name,@RequestParam(required = false) Date startDate){
        return "request params is " + name + " , date and time " + startDate;
    }

    @RequestMapping("/doHandleRequestParam03/{id}/{code}")
    public String doHandleRequestParam03(@PathVariable Integer id,@PathVariable String code){
        return "request params is " + id + code;
    }

    @RequestMapping("/doHandleRequestParam04")
    public String doHandleRequestParam04(RequestParameter pojo){
        return "request params"+pojo.toString();
    }

    @RequestMapping("/doHandleRequestParam05/{name}/{startDate}")
    public String doHandleRequestParam05(RequestParameter pojo){
        return "request params"+pojo.toString();
    }
    //假如客户端请求参数比较多但是不想定义pojo接受请求参数，那应该如何实现
    //可以使用map但是一般不推荐，map一般无法对请求参数进行限制
    //假如使用map接受请求参数，需要在方法参数前使用@RequestParam注解进行描述
    @RequestMapping("/doHandleRequestParam06")
    public String doHandleRequestParam06(@RequestParam Map<String,Object> map){
        return "request params"+map.toString();
    }
    //rest风格url参数
    //当Map接受rest风格中的数据时，需要使用@pathVariable注解对map参数进行描述
    //不能使用@RequestParam注解因为此注解描述的参数只能接受普通url中？后面 定义的参数值
    @RequestMapping("/doHandleRequestParam07/{name}/{startDate}")
    public String doHandleRequestParam07(@PathVariable Map<String,Object> map){
        return "request params"+map.toString();
    }
}
